package  
{
	import collisions.Body;
	import collisions.World;
	import enemies.RayPath;
	import enemies.Strategy;
	import entities.Entity;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import recorder.EntityStateRecorder;
	import recorder.RecordData;
	
	/**
	 * ...
	 * @author Gareth Williams
	 */
	public class Bullet extends Entity 
	{
		//state machine properties
		private var state:Strategy = new Strategy();
		
		//state properties recorder
		private var _recorder:EntityStateRecorder;
		
		public function Bullet(rotation:Number, x:Number = 0, y:Number = 0, radius:Number = 10) 
		{
			this.collisionGroup = Constants.BULLET_GROUP;
			
			super(x, y, radius);
			this.rotation = rotation;
			this.speed = 10;
			_recorder = new EntityStateRecorder(this);
			state.implementation = new RayPath();
		}
		override public function destructor():void 
		{
			state = null; //release reference
			super.destructor();
		}
		override public function update():void 
		{
			//apply recorded states
			_recorder.update();
			var data:RecordData = _recorder.retrieveDataAt(Main.timestep);
			data.applyTo(this);
			
			if (false == isAlive)
			{
				DisplayObject(graphic).visible = false;
		//profiling
				World.removeBody(this);
			}
			else
			{
				DisplayObject(graphic).visible = true;
		//profiling
				World.addBody(this);
			}
			
			//shortcut if this is dead...
			if (isAlive == false) return;
				
			//strategy
			var strategyCompleted:Boolean = state.perform(this);
		//profiling
			if (true == strategyCompleted && World.bodies[this])
			{
				World.removeBody(this);
			}
		}
		override public function doRender():Boolean
		{
			return DisplayObject(graphic).visible;
		}
		override public function processCollisionWith(collider:Body):void //Body
		{	
			this.isAlive = false;
		}
	}
}